/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import dao.DBManager;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import model.ErrorMessage;
import ui.Detail;
import ui.TextItem;

/**
 *
 * @author kien.tran
 */
public class ConfirmForm extends AForm implements ICommandListener {

    private Detail detail;
    private String title;
    private String content;

    public ConfirmForm() {
    }

    public ConfirmForm(MStockMidlet control, Display display, int branch, int option) {
        super(control, display);
        this.branch = branch;
        this.option = option;
    }

    protected void initData() {
        lang = new String[]{"Cập nhật dữ liệu", "Bạn đồng ý cập nhật dữ liệu?",
                            "Thông báo", "Bạn muốn thoát khỏi ứng dụng?"};

        recycle = control.getRecycle();

        if (branch == State.CONFIG){
            if (option == State.DATA){
                title = lang[0];
                content = lang[1];
                icon = control.getIcons()[7];
            }
        } else if (branch == State.EXIT){
            title = lang[2];
            content = lang[3];
        }
    }

    protected void initGui() {
        detail = new Detail(display, control, title, background, icon);
        ok = new Command("Đồng ý", Command.OK);
        back = new Command("Trở về", Command.BACK);
        detail.setSelectCommand(ok);
        detail.setCancelCommand(back);
        detail.setCommandListener(this);

        showData();
    }

    protected void loadData() {
    }

    protected void showData() {
        this.detail.addItem(new TextItem("-1", "", content));
        this.detail.show();
    }

    protected void showError(ErrorMessage error) {
    }

    public void commandAction(Command command) {
        if (command == ok) {
            if (branch == State.CONFIG){
                if (option == State.DATA){

                    // truncate all databases
                    DBManager.truncate(DBManager.DB_HELP);

                    // empty all objects of recycle data
                    recycle.empty();

                    new InfoForm(control, display, branch, option).show();
                }
                
            } else if (branch == State.EXIT){
                control.destroyApp(true);
                control.notifyDestroyed();
            }
            
        } else if (command == back) {
            control.back(0);
        }
    }
}
